import { withUser, parseBody, json, ApiError } from "@/lib/api"; import { getEndpoint, updateEndpoint, deleteEndpoint, ENDPOINT_PATCH_SCHEMA } from "@/lib/endpoints/service"; import { LIMITS } from "@/lib/rate-limit"; export const dynamic = "force-dynamic"; type P = { id: string }; /** GET /api/endpoints/[id] → `{ endpoint: PublicEndpoint }` */ export const GET = withUser
(async ({ user }, { id }) => { const endpoint = await getEndpoint(user.id, id); if (!endpoint) throw new ApiError(404, "Endpoint not found", "NOT_FOUND"); return json({ endpoint }); }); /** PATCH /api/endpoints/[id] — partial `EndpointInput` (apiKey `null` clears it) → `{ endpoint }` */ export const PATCH = withUser
( async ({ req, user, ip }, { id }) => { const body = await parseBody(req, ENDPOINT_PATCH_SCHEMA, 64_000); return json({ endpoint: await updateEndpoint(user.id, id, body, ip) }); }, { limit: { ...LIMITS.keySave, key: "endpoint-save" } }, ); /** DELETE /api/endpoints/[id] → `{ ok: true }` */ export const DELETE = withUser
(async ({ user, ip }, { id }) => { await deleteEndpoint(user.id, id, ip); return json({ ok: true }); });